home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 939 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  85 lines

  1. Path: news.cs.utah.edu!usenet
  2. From: bendixen@eng.utah.edu (Mason Bendixen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Problems with inheritance and templates in g++
  5. Date: 8 Jan 1996 15:46:23 GMT
  6. Organization: University of Utah Computer Science Department
  7. Message-ID: <4cre8f$bkp@magus.cs.utah.edu>
  8. Reply-To: bendixen@eng.utah.edu
  9. NNTP-Posting-Host: cadesm48.eng.utah.edu
  10.  
  11. Hi,
  12.      During the link stage, g++ complains about
  13. undefined symbols that correspond to unused member
  14. functions of instantiated objects. The code in question
  15. compiles fine under Borland C++ 4.5 but not g++.
  16. Take the sample file:
  17.  
  18. // BEGIN SAMPLE PROGRAM
  19.  
  20. #include <iostream.h>
  21.  
  22. template <class T>
  23. class base {
  24. public:
  25.     base () {}
  26.     virtual int a() = 0;
  27.     virtual int b() = 0;
  28.     static base<T>* make();
  29. };
  30.  
  31. template <class T>
  32. class test : public base<T> {
  33. public:
  34.     test() {}
  35.     int a();
  36.     int b();
  37. };
  38.  
  39. template <class T>
  40. base<T>* base<T>::make() {return new test<T>;}
  41.  
  42. template <class T>
  43. int test<T>::a() {return 1;}
  44.  
  45. template <class T>
  46. int test<T>::b() {return 2;}
  47.  
  48. int main() {
  49.     test<int> b;
  50.     //int i = b.a();
  51.     //int j = b.b();
  52.     base<int>* t = base<int>::make();
  53.     cout << t->a() << endl;
  54.     return 0;
  55. }
  56.  
  57. // END SAMPLE PROGRAM
  58.  
  59.  
  60. When I try to compile I get:
  61.  
  62. >> 97 cadesm48% g++ -o tester temp.C
  63. >> collect2: ld returned 2 exit status
  64. >> ld: Undefined symbol
  65. >>    test<int>::a(void)
  66. >>    test<int>::b(void)
  67.  
  68. If I uncomment the following lines:
  69.  
  70. >>      int i = b.a();
  71. >>      int j = b.b();
  72.  
  73. The program will work just fine.
  74. Can I force g++ to instantiate all member of every
  75. instantiated object? I think this has something to
  76. do with the way g++ handles inheritance for templatized
  77. classes. Any suggestions, insight, or possible work
  78. a rounds in this age of pre- standardized C++ would be
  79. greatly appreciated.
  80.  
  81. Thanks
  82.    Mason Bendixen
  83.  
  84.  
  85.